AddCSLuaFile ("cl_init.lua")
AddCSLuaFile ("shared.lua")

include ("shared.lua")

ENT.LaunchSound = Sound ("Missile.Accelerate")
ENT.ExplodeSound = Sound ("explode4")

function ENT:Initialize ()
	self.Entity:SetModel ("models/Weapons/W_missile_closed.mdl") 
	
    self.Entity:PhysicsInit (SOLID_VPHYSICS)
    self.Entity:SetMoveType (MOVETYPE_VPHYSICS)   
    self.Entity:SetSolid (SOLID_VPHYSICS)
	self.SpawnTime = CurTime()
	self.Launched = false
	
	self.PhysObj = self.Entity:GetPhysicsObject()
    if (self.PhysObj:IsValid()) then
		self.PhysObj:SetMass(1)
		self.PhysObj:EnableGravity(true)
        self.PhysObj:Wake()
    end
	
	self.LastDeviation = CurTime()
	
	local ais = ents.Create ("ai_sound")
	ais:SetKeyValue ("duration", 5)
	ais:SetKeyValue ("volume", 1200)
	ais:SetPos (self:GetPos())
	ais:SetParent (self.Entity)
	ais:Spawn()
	ais:Fire ("EmitAISound", "", 0)
	self.AISound = ais
end

function ENT:Think ()
	if not self.Launched and self.SpawnTime + 0.05 < CurTime() then
		self:Launch()
		self.Launched = true
		self.Entity:SetNWBool ("l", true)
	elseif self.Launched then
		if self.SpawnTime + 5 < CurTime() then self:Explode() return end
		local vecVelocity = self.Entity:GetForward() * 1000
		self.PhysObj:ApplyForceCenter (vecVelocity)
		if self.LastDeviation + 0.2 < CurTime() then
			print ("deviation: "..self.Deviation)
			self.PhysObj:AddAngleVelocity (Vector ((math.random() - 0.5) * 100 * self.Deviation,(math.random() - 0.5) * 100 * self.Deviation,(math.random() - 0.5) * 100 * self.Deviation) - self.PhysObj:GetAngleVelocity())
			self.LastDeviation = CurTime()
		end
	end
	self.Entity:NextThink (CurTime() + 0.01)
end

function ENT:PhysicsUpdate()
	for k,v in pairs (ents.FindByClass ("npc_helicopter")) do
			local lPos = v:WorldToLocal (self.Entity:GetPos())
			local maxs = Vector (150, 150, 150)
			local mins = Vector (-150, -150, -150)
			print (lPos, maxs, mins)
			if lPos.x > mins.x and lPos.y > mins.y and lPos.z > mins.z then
				print ("above mins")
				if lPos.x < maxs.z and lPos.y < maxs.y and lPos.z < maxs.z then
					print ("below max")
					self:Explode()
				end
			end
		end
end

function ENT:Launch ()
	local vecVelocity = self.Entity:GetForward() * 250
	self.PhysObj:ApplyForceCenter (vecVelocity)
	self.PhysObj:EnableGravity(false)
	self.Entity:EmitSound (self.LaunchSound)
end

function ENT:PhysicsCollide () self:Explode () end

function ENT:Explode ()
	if self.Exploded then return end
	printd ("xplosion! ("..CurTime()..")")
	self.Entity:StopSound (self.LaunchSound)
	--[[local explo = ents.Create ("env_explosion")
		explo:SetOwner (self.Owner)
		explo:SetPhysicsAttacker (self.Owner)
		explo:SetPos (self.Entity:GetPos())
		explo:SetKeyValue ("iMagnitude", 60)
		explo:Spawn()
		explo:Activate()
		explo:Fire ("Explode", "", 0)]]
	local vPoint = self.Entity:GetPos()
	local effectdata = EffectData()
	effectdata:SetStart (vPoint)
	effectdata:SetOrigin (vPoint)
	util.Effect ("Explosion", effectdata)
	printd ("damage: "..self.Damage)
	util.BlastDamage (self.Entity, self.Attacker, self.Entity:GetPos(), self.Radius, self.Damage)
	self.Exploded = true
	self.Entity:Fire ("kill", "", 0)
end
